//获取应用实例
var app = getApp()
var timer;
Page({
  data: {
    seckillshow: 0,
    showLoading: true,
    noMoretip: false,
    page: 0,
    hours: '00',
    minutes: '00',
    seconds: '00',
    startTime: '',
    endTime: '',
    timeactive: 0,
  },
  onLoad: function (e) {
    var that = this;
    if (e && e.enterfrom) {
      var enterfrom = e.enterfrom == 'share' ? true : false;
      that.setData({
        enterfrom: enterfrom
      })
    }
    that.requestIndex();
    app.requestAd(that);
  },
  requestIndex: function () {
    var that = this;
    wx.request({
      url: app.globalData.requestUrl,
      data: {
        map: 'applet_mall_limit_index',
      },
      success: function (res) {
        console.log(res.data.data);
        if (res.data.ec == 200) {
          if (res.data.data.activity.length > 0) {
            that.setData({
              shopGoods: res.data.data.activity[0].goodsList,
              startTime: res.data.data.activity[0].startTime,
              endTime: res.data.data.activity[0].endTime,
              status: res.data.data.activity[0].status
            })
            var status = res.data.data.activity[0].status;
            that.countDjs(that.data.startTime, that.data.endTime, status, 0);
          } else {
            that.setData({
              shopGoods: []
            })
          }
          var categoryList = app.splitArrData(res.data.data.shortcut, 8);
          that.setData({
            tempInfo: res.data.data.template,
            slideImgUrls: res.data.data.slide,
            shortMenu: res.data.data.shortcut,
            categoryList: categoryList,
            coupon: res.data.data.coupon,
            activity: res.data.data.activity,
            titleName: res.data.data.template.title,
          })
          if (res.data.data.template.title) {
            wx.setNavigationBarTitle({
              title: res.data.data.template.title
            });
          }
        } else {
          console.log(res.data)
        }
      },
      complete: function () {
        wx.hideToast();
        wx.stopPullDownRefresh();
      }
    });
  },
  skillGoodList: function () {
    var that = this;
    var page = that.data.page;
    var time = that.data.timeChange;
    console.log(time)
    wx.request({
      url: app.globalData.requestUrl,
      data: {
        map: 'applet_mall_limit_goods',
        time: time,
        page: page
      },
      success: function (res) {
        if (res.data.ec == 200) {
          var allArr = [];
          var initArr = that.data.shopGoods ? that.data.shopGoods : '[]';
          var curArr = res.data.data;
          var lastPageLength = curArr.length;
          if (page > 0) {
            allArr = initArr.concat(curArr);
          } else {
            allArr = res.data.data;
          }
          that.setData({
            shopGoods: allArr
          })
          if (lastPageLength <= 10) {
            that.setData({
              noMoretip: true,
              showLoading: false
            });
            // console.log()
          }
          console.log(that.data.shopGoods);
        } else {
          console.log(res.data)
          if (page <= 0) {
            that.setData({
              shopGoods: []
            })
          } else {
            that.setData({
              noMoretip: true,
              showLoading: false
            });
          }
        }
      },
      complete: function () {
        wx.hideToast();
        wx.stopPullDownRefresh();
      }
    });
  },
  onPullDownRefresh: function () {
    var that = this;
    clearInterval(that.data.timer);
    this.setData({
      page: 0,
      noMoretip: false,
      showLoading: true,
      timeactive: 0
    });
    that.requestIndex();
    console.log("下拉刷新");
  },
  timeToggle: function (e) {
    var that = this;
    clearInterval(that.data.timer);
    var index = e.currentTarget.dataset.id;
    var shopGoods = e.currentTarget.dataset.goods;
    var startTime = e.currentTarget.dataset.starttime;
    var endTime = e.currentTarget.dataset.endtime;
    var status = e.currentTarget.dataset.status;
    console.log(startTime)
    console.log(endTime)
    var timeactive = e.currentTarget.dataset.id;
    that.setData({
      timeactive: timeactive,
      startTime: startTime,
      endTime: endTime,
      shopGoods: shopGoods
    });
    // that.requestIndex();
    that.countDjs(startTime, endTime, status, index);
  },
  // 领取优惠券跳转
  getCoupon: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    console.log(id);
    wx.showLoading({
      title: '领取中',
      mask: true
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: {
        map: 'applet_coupon_receive',
        cid: id
      },
      success: function (res) {
        console.log(res.data);
        if (res.data.ec == 200) {
          app.errorTip(that, res.data.data.msg, 2000);
        } else {
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    })
  },
  openGoodDetail: function (e) {
    var that = this;
    var goodid = e.currentTarget.dataset.id;
    var laid = e.currentTarget.dataset.laid;
    wx.navigateTo({
      url: '/subpages1/goodDetail/goodDetail?id=' + goodid + '&laid=' + laid,
    })
  },
  openSeckillList: function (e) {
    var that = this;
    var category = e.currentTarget.dataset.id;
    var title = e.currentTarget.dataset.title;
    wx.navigateTo({
      url: '/subpages1/seckillGoodList/seckillGoodList?id=' + category + '&title=' + title
    })
  },
  searchPage: function () {
    wx.navigateTo({
      url: '/subpages1/searchSeckill/searchSeckill'
    })
  },
  countDjs: function (start, end, status, index) {
    var that = this;
    // console.log(start);
    // console.log(end);
    var startTime = start * 1000;
    var endTime = end * 1000;
    var curtime = new Date();  //避免页面渲染倒计时的时候出现一秒的延迟。
    var startSpan1 = startTime - curtime;
    var endSpan2 = endTime - curtime;
    if (startSpan1 > 0) {
      that.setData({
        start: '开始',
      })
      that.djs(startSpan1, status, index, start, end);
    } else {
      that.setData({
        start: '结束',
      })
      that.djs(endSpan2, status, index);
    }
    that.data.timer = setInterval(function () {
      var nowTime = new Date();
      var startSpan = startTime - nowTime;
      var endSpan = endTime - nowTime;
      if (startSpan > 0) {
        that.setData({
          start: '开始',
        })
        that.djs(startSpan, status, index, start, end);
      } else {
        that.setData({
          start: '结束',
        })
        that.djs(endSpan, status, index);
      }
    }, 1000)
  },
  djs: function (time, status, index, start, end) {
    var that = this;
    // var leftTime = (new Date("2017/09/13 17:59:59")) - (new Date()); //计算剩余的毫秒数 
    var leftTime = time;
    that.setData({
      leftTime: leftTime
    })
    // console.log(leftTime);
    if (leftTime > 0) {
      var days = parseInt(leftTime / 1000 / 60 / 60 / 24, 10); //计算剩余的天数 
      var hours = parseInt(leftTime / 1000 / 60 / 60 % 24, 10); //计算剩余的小时 
      var minutes = parseInt(leftTime / 1000 / 60 % 60, 10);//计算剩余的分钟 
      var seconds = parseInt(leftTime / 1000 % 60, 10);//计算剩余的秒数 
      days = days < 10 ? '0' + days : days;
      // console.log(days);
      hours = hours < 10 ? '0' + hours : hours;
      minutes = minutes < 10 ? '0' + minutes : minutes;
      seconds = seconds < 10 ? '0' + seconds : seconds;
      that.setData({
        days: days,
        hours: hours,
        minutes: minutes,
        seconds: seconds
      })
      if (days == '00' && hours == '00' && minutes == '00' && seconds == '00') {
        clearInterval(that.data.timer);
        var activity = that.data.activity;
        console.log("当前状态" + status);
        console.log("当前秒杀索引" + index)
        console.log(activity[index].statusNote);
        if (status == 0) {
          activity[index].status = 1;
          activity[index].statusNote = '进行中';
          console.log(activity[index].statusNote);
          var shopGoods = activity[index].goodsList;
          for (let i = 0; i < shopGoods.length; i++) {
            shopGoods[i].status = 1;
          }
          that.setData({
            activity: activity,
            shopGoods: shopGoods
          })
          console.log(that.data.shopGoods);
          that.countDjs(start, end, 1, index);
        }

      }
    } else {
      that.setData({
        days: '00',
        hours: '00',
        minutes: '00',
        seconds: '00'
      })
    }
  },
  formatTime: function (leftTime) {//格式化显示时间
    var that = this;
    var days = parseInt(leftTime / 60 / 60 / 24, 10); //剩余的天数 
    var hours = parseInt(leftTime / 60 / 60 % 24, 10); //剩余小时 
    var minutes = parseInt(leftTime / 60 % 60, 10);//剩余分钟 
    var seconds = parseInt(leftTime % 60, 10);//剩余秒数 
    days = days < 10 ? '0' + days : days;
    hours = hours < 10 ? '0' + hours : hours;
    minutes = minutes < 10 ? '0' + minutes : minutes;
    seconds = seconds < 10 ? '0' + seconds : seconds;
    that.setData({
      days: days,
      hours: hours,
      minutes: minutes,
      seconds: seconds
    });
  },
  toCuslink: function (e) {
    var link = e.currentTarget.dataset.link;
    console.log(link);
    wx.navigateTo({
      url: link
    })
  },
  onShareAppMessage: function () {
    var that = this;
    var title = that.data.titleName;
    var shareInfo = app.globalData.shareInfo;
    title = shareInfo.shareTitle ? shareInfo.shareTitle : title;
    var cover = shareInfo.shareCover ? shareInfo.shareCover : '';
    app.getPoint(that);
    return {
      title: title,
      imageUrl: cover,
      path: '/subpages1/seckillPageShow/seckillPageShow?enterfrom=share'
    }
  }
})
